<?php

namespace App\Http\Controllers;

use App\Models\BankCard;
use App\Models\DoctorWorks;
use App\Models\Follow;
use App\Models\ProductGoods;
use App\Util\Tool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\BaseController;

class DoctorWorksController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     * 医护工作室列表
     */
    public function index(Request $request)
    {
        $param = $request->input();
        if (empty($param['lng']) || empty($param['lat'])) {
            return $this->ResultJson(0, '经纬度不能为空!', []);
        }
        $course = (new DoctorWorks())->getWorkList($param);
        return $this->ResultJson(1, '获取成功!', $course);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *
     * 关注
     */

    public function addFollow(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '医护室ID不能为空!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        $course = (new Follow())->setWorkFollow($param);
        switch ($course) {
            case 1:
                return $this->ResultJson(1, '取消关注成功!', []);
                break;
            case 2:
                return $this->ResultJson(0, '取消关注失败!', []);
                break;
            case 3:
                return $this->ResultJson(1, '关注成功!', []);
                break;
            case 4:
                return $this->ResultJson(0, '关注失败!', []);
                break;
            case 5:
                return $this->ResultJson(0, '该用户没有病例,请先建病例后再关注!', []);
                break;
        }
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取关注医护室列表
     */

    public function getFollowList(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        if (empty($param['lng'])) {
            return $this->ResultJson(0, '精度不能为空!', []);
        }
        if (empty($param['lat'])) {
            return $this->ResultJson(0, '纬度不能为空!', []);
        }
        $course = (new Follow())->getFollowList($param);
        return $this->ResultJson(1, '获取成功!', $course);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *
     * 入驻医护工作室
     */
    public function create(Request $request)
    {
        $res = $request->input() ?: [];
        $param = $this->verify($res);
        if ($param === true) {
            $result = (new DoctorWorks())->createDoctorWork($res);
            if (is_array($result)) {
                return $this->ResultJson(1, '申请成功!', $result);
            } else {
                return $this->ResultJson(0, $result, []);
            }
        }
        return $this->ResultJson(0, $param, []);;
    }


    /****
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 数据验证
     */
    public function verify($res)
    {
        if (empty($res['user_id'])) {
            return '用户id不能为空!';
        }
        if (empty($res['phone'])) {
            return '手机号不能为空!';
        }
        //判断验证码不能为空
        if (empty($res['code'])) {
            return '验证码不能为空!';
        }
        //判断短信验证码是否过期
        $code = Tool:: GetSmsvalidator($res['phone']);
        if (empty($code)) {
            return '短信验证码已经过期';
        }
        //需要检查短信验证码是正确
        if ($res['code'] != $code) {
            return  '短信验证码错误';
        }
        //更新验证码为验证过了
        $this->SetCode($res['phone'], $code);

        if (empty($res['account'])) {
            return '账号不能为空!';
        }
        if (empty($res['password'])) {
            return '密码不能为空!';
        }
        if (empty($res['work_name'])) {
            return '工作室名称不能为空!';
        }
        if (empty($res['province'])) {
            return '省份不能为空!';
        }
        if (empty($res['city'])) {
            return '市不能为空!';
        }
        if (empty($res['county'])) {
            return '县不能为空!';
        }
        if (empty($res['address'])) {
            return '详细地址不能为空!';
        }
        if (empty($res['logo'])) {
            return 'Logo不能为空!';
        }
        if (empty($res['work_introduce'])) {
            return '简介不能为空!';
        }
        if (empty($res['work_detail'])) {
            return '详情不能为空!';
        }

        ///////医护信息//////////
        if (empty($res['real_name'])) {
            return '医护姓名不能为空!';
        }
        if (empty($res['idcard'])) {
            return '身份证号不能为空!';
        }
        if (empty($res['clinic_id'])) {
            return '科室ID不能为空!';
        }

        if (empty($res['professional_id'])) {
            return '职称不能为空!';
        }
        if (empty($res['hospital'])) {
            return '所属医院不能为空!';
        }
        if (empty($res['front_card'])) {
            return '身份证正面不能为空!';
        }
        if (empty($res['age'])) {
            return '年龄不能为空!';
        }
        if (empty($res['sex'])) {
            return '性别不能为空!';
        }
        if (empty($res['behind_card'])) {
            return '身份证反面不能为空!';
        }
        if (empty($res['profile'])) {
            return '头像不能为空!';
        }
        if (empty($res['certificate'])) {
            return '职业资质证书不能为空!';
        }
        if (empty($res['introduce'])) {
            return '简介不能为空!';
        }
        if (empty($res['skill'])) {
            return '擅长不能为空!';
        }
        return true;
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * 获取商品列表
     *
     */
    public function getGoodsList(Request $request)
    {
        $param  = $request->input();
        $res = (new ProductGoods())->getGoodsList($param);
        return  $this->ResultJson(1,'获取成功!',$res);
    }


    /**
     * 商品详情
     */
    public function getGoodsDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室id不能为空!', []);
        }
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品id不能为空!', []);
        }
        if (empty($param['collect_type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        $imgs = (new ProductGoods())->getGoodsDetail($param);
        return $this->ResultJson(1, '获取成功!', $imgs);

    }




    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * 获取康养商品列表
     *
     */
    public function getHealthGoodsList(Request $request)
    {
        $param  = $request->input();
        $res = (new ProductGoods())->getHealthGoodsList($param);
        return  $this->ResultJson(1,'获取成功!',$res);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * 获取康养商品分类
     *
     */
    public function getHealthCategoryList(Request $request)
    {
        $param  = $request->input();
        $res = (new ProductGoods())->getHealthCategoryList($param);
        return  $this->ResultJson(1,'获取成功!',$res);
    }



    /**
     * Display the specified resource.
     *
     * @param  \App\Models\DoctorWorks $doctorWorks
     * @return \Illuminate\Http\Response
     * 工作室详情
     */
    public function show(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $result = (new DoctorWorks())->detail($param);
        return $this->ResultJson(1, '获取成功!', $result);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\DoctorWorks $doctorWorks
     * @return \Illuminate\Http\Response
     */
    public function getDoctorByWorkId(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $result = (new DoctorWorks())->getDoctorByWorkId($param);
        return $this->ResultJson(1, '获取成功!', $result);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\DoctorWorks $doctorWorks
     * @return \Illuminate\Http\Response
     * ///修改广告图
     */
    public function update(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['images'])) {
            return $this->ResultJson(0, '广告图不能为空!', []);
        }
        $res = (new DoctorWorks())->updateImage($param);
        if ($res) {
            return $this->ResultJson(1, '设置成功!', []);
        } else {
            return $this->ResultJson(0, '设置失败!', []);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 修改工作室信息
     */


   public function updateWorkInfo(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
//        if (empty($param['images'])) {
//            return $this->ResultJson(0, '广告图不能为空!', []);
//        }
        $res = (new DoctorWorks())->updateWorkInfo($param);
        if ($res) {
            return $this->ResultJson(1, '修改成功!', []);
        } else {
            return $this->ResultJson(0, '修改失败!', []);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\DoctorWorks $doctorWorks
     * @return \Illuminate\Http\Response
     * 修改工作地址
     */
    public function updateWorkAddress(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['province_id'])) {
            return $this->ResultJson(0, '省Code不能为空!', []);
        }
        if (empty($param['city_id'])) {
            return $this->ResultJson(0, '市Code不能为空!', []);
        }
        if (empty($param['district_id'])) {
            return $this->ResultJson(0, '县Code不能为空!', []);
        }
        if (empty($param['address'])) {
            return $this->ResultJson(0, '详细地址不能为空!', []);
        }
        $res =  (new DoctorWorks())->updateImage($param);
        if ($res) {
            return $this->ResultJson(1, '设置成功!', []);
        } else {
            return $this->ResultJson(0, '设置失败!', []);
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\DoctorWorks $doctorWorks
     * @return \Illuminate\Http\Response
     * 工作室绑定账号
     */
    public function bindBankCard(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['card_name'])) {
            return $this->ResultJson(0, '名称不能为空!', []);
        }
        if (empty($param['branch'])) {
            return $this->ResultJson(0, '开户支行不能为空!', []);
        }
        if (empty($param['open_bank'])) {
            return $this->ResultJson(0, '开户行不能为空!', []);
        }
        if (empty($param['card_num'])) {
            return $this->ResultJson(0, '卡号不能为空!', []);
        }
        $res =  (new BankCard())->add($param);
        if ($res) {
            return $this->ResultJson(1, '绑定成功!', []);
        } else {
            return $this->ResultJson(0, '绑定失败!', []);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\DoctorWorks $doctorWorks
     * @return \Illuminate\Http\Response
     * 工作室账号详情
     */
    public function bankCardInfo(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $res =  (new BankCard())->detail($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!',$res);
        } else {
            return $this->ResultJson(0, '获取失败!', []);
        }
    }
}
